package client.ui.theme.defaultTheme;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import client.engine.ChatEngine;
import client.ui.ILoginWindow;

public class DefaultLoginWindow extends JFrame implements ILoginWindow{
	
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton loginButton;
    private javax.swing.JTextField loginUserName;
    private javax.swing.JTextField loginUserName2;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JPasswordField passwordField2;
    private javax.swing.JPasswordField passwordFieldConfirm;
    private javax.swing.JButton registerButton;
	private ChatEngine myClient;
	
	ClassLoader cldr1 = this.getClass().getClassLoader();
	java.net.URL imageURL1   = cldr1.getResource("client/ui/theme/defaultTheme/Parlanchin.jpg");
	
	final ImageIcon logo = new ImageIcon(imageURL1);


	public DefaultLoginWindow()
	{
		initComponents();
		
		this.setLocationRelativeTo(null);
	}

	 private void initComponents() {

	        jTabbedPane1 = new javax.swing.JTabbedPane();
	        jPanel1 = new javax.swing.JPanel();
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        loginUserName = new javax.swing.JTextField();
	        jLabel3 = new javax.swing.JLabel();
	        loginButton = new javax.swing.JButton();
	        jLabel8 = new javax.swing.JLabel();
	        jLabel9 = new javax.swing.JLabel();
	        passwordField = new javax.swing.JPasswordField();
	        jPanel2 = new javax.swing.JPanel();
	        jLabel4 = new javax.swing.JLabel();
	        jLabel5 = new javax.swing.JLabel();
	        loginUserName2 = new javax.swing.JTextField();
	        jLabel6 = new javax.swing.JLabel();
	        jLabel7 = new javax.swing.JLabel();
	        registerButton = new javax.swing.JButton();
	        passwordField2 = new javax.swing.JPasswordField();
	        passwordFieldConfirm = new javax.swing.JPasswordField();
	        
	        

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        jLabel1.setIcon(logo); // NOI18N

	        jLabel2.setText("Screen Name:");

	        jLabel3.setText("Password:");

	        loginButton.setText("Login");
	        loginButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                loginButtonActionPerformed(evt);
	            }
	        });

	        jLabel8.setText("* Are you a new user? Hit the Register tab to");

	        jLabel9.setText("register a new Screen Name and Password!");

	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	        jPanel1.setLayout(jPanel1Layout);
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(loginUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
	                            .addComponent(jLabel2)
	                            .addComponent(jLabel3)
	                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(78, 78, 78)
	                        .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(20, 20, 20)
	                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
	                            .addComponent(jLabel9)
	                            .addComponent(jLabel8)))
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
	                .addContainerGap())
	        );
	        jPanel1Layout.setVerticalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addComponent(jLabel1)
	                .addGap(29, 29, 29)
	                .addComponent(jLabel2)
	                .addGap(3, 3, 3)
	                .addComponent(loginUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(jLabel3)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(13, 13, 13)
	                .addComponent(jLabel8)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(jLabel9)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
	                .addComponent(loginButton)
	                .addGap(19, 19, 19))
	        );

	        jTabbedPane1.addTab("Login", jPanel1);

	        jLabel4.setIcon(logo); // NOI18N

	        jLabel5.setText("Screen Name:");

	        jLabel6.setText("Password:");

	        jLabel7.setText("Re-enter Password:");

	        registerButton.setText("Register");
	        registerButton.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                registerButtonActionPerformed(evt);
	            }
	        });

	        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
	        jPanel2.setLayout(jPanel2Layout);
	        jPanel2Layout.setHorizontalGroup(
	            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel2Layout.createSequentialGroup()
	                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(jPanel2Layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                            .addComponent(jLabel5)
	                            .addComponent(loginUserName2)
	                            .addComponent(jLabel6)
	                            .addComponent(jLabel7)))
	                    .addGroup(jPanel2Layout.createSequentialGroup()
	                        .addGap(79, 79, 79)
	                        .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
	                    .addGroup(jPanel2Layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addComponent(passwordField2, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
	                    .addGroup(jPanel2Layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addComponent(passwordFieldConfirm, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
	                .addContainerGap())
	        );
	        jPanel2Layout.setVerticalGroup(
	            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel2Layout.createSequentialGroup()
	                .addComponent(jLabel4)
	                .addGap(32, 32, 32)
	                .addComponent(jLabel5)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(loginUserName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(jLabel6)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(passwordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(jLabel7)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addComponent(passwordFieldConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(13, 13, 13)
	                .addComponent(registerButton)
	                .addContainerGap(23, Short.MAX_VALUE))
	        );

	        jTabbedPane1.addTab("Register", jPanel2);

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
	        );

	        pack();
	    }// </editor-fold>

	@Override
	public void createAccount(String username, String password) {
		// TODO Auto-generated method stub
		myClient.createAccount(username, password);
		
	}

	@Override
	public void login(String username, String password) {
		// TODO Auto-generated method stub
		myClient.signOn(username, password);
	}

	@Override
	public void setChatEngine(ChatEngine engine) {

		this.myClient = engine;
		
	}

	@Override
	public void showError(String error) {

		JOptionPane.showMessageDialog(this, error);
		
	}
	
	public void showBuddyList()
	{	
		this.dispose();
	}
	
	public void firePasswordMatchFail()
	{
		JOptionPane.showMessageDialog(this, 
		"Passwords don't match.");
	}

	public JTextField getRegisterNameField()
	{
		return loginUserName2;
	}
	
	public JTextField getRegisterPassField()
	{
		return passwordField2;
	}
	
	public JTextField getConfirmPassField()
	{
		return passwordFieldConfirm;
	}
	
	public JButton getRegisterButton()
	{
		return registerButton;
	}

	@Override
	public void clearWindow() {
		
		getRegisterNameField().setText("");
		getRegisterPassField().setText("");
		getConfirmPassField().setText("");
		
	}

	@Override
	public void setWaitingState(boolean state) {
		
		getRegisterButton().setEnabled(state);
		
	}
	
	public void loginKeyPressed(KeyEvent e) {
	    int key = e.getKeyCode();
	    if (key == KeyEvent.VK_ENTER) {
	       loginButtonActionPerformed(null);
	    }
	}
	
	public void registerKeyPressed(KeyEvent e) {
	    int key = e.getKeyCode();
	    if (key == KeyEvent.VK_ENTER) {
	       registerButtonActionPerformed(null);
	    }
	}
	
	 private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
		 String signOnUsername = loginUserName.getText();
			String signOnPassword = passwordField.getText();
			
			login(signOnUsername, signOnPassword);
			
			showBuddyList();
	}
	 
	 private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {
		 String registerUsername = loginUserName2.getText();
			String registerPassword = passwordField2.getText();
			String confirmRegisterPassword = passwordFieldConfirm.getText();
			
			if(registerPassword.equals(confirmRegisterPassword))
			{
				registerButton.setEnabled(false);
				createAccount(registerUsername, registerPassword);
			}
			else
			{
				firePasswordMatchFail();
			}
	}
	
}
